﻿using System;
using ESRI.ArcGIS.Client;
namespace Services.Maps
{
    public class OpenStreetLayer : TiledMapServiceLayer
    {
        private const double cornerCoordinate = 20037508.342787;
        public override void Initialize()
        {

            this.FullExtent = new
           ESRI.ArcGIS.Client.Geometry.Envelope(-20037508.342787, -20037508.342787, 20037508.342787, 20037508.342787);
            {
                SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100);
            };


            this.SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100);

            this.TileInfo = new TileInfo()
            {
                Height = 256,
                Width = 256,

                Origin = new ESRI.ArcGIS.Client.Geometry.MapPoint(-20037508.342787, 20037508.342787)
                {
                    SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100)
                },
                Lods = new Lod[20]
            };


            double resolution = 156543.033928;

            for (int i = 0; i < TileInfo.Lods.Length; i++)
            {

                TileInfo.Lods[i] = new Lod() { Resolution = resolution };
                resolution /= 2;
            }

            // Call base initialize to raise the initialization event 
            base.Initialize();
        }
        Random random = new Random();
        public override string GetTileUrl(int level, int row, int col)
        {
            string baseUrl = "http://{0}.tile.openstreetmap.org/{3}/{1}/{2}.png";

            var v = "abc"[random.Next(0, 2)].ToString();
            return string.Format(baseUrl, v, col, row, level);
        }

    }
}